//
//  Tweet.m
//  TuiTe
//
//  Created by 张 霄鹤 on 10-3-12.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "Tweet.h"
#import "DataManager.h"

@implementation Tweet

@dynamic created_at;
@dynamic tid;
@dynamic text;
@dynamic source;
@dynamic truncated;
@dynamic favorited;
@dynamic unreaded;
@dynamic usr;

- (void)parse:(Element *)el{
	Node *nd = [el getChildByName:@"created_at"];
	if(nd){
		self.created_at = [[nd getChildByName:@"#TEXT"] getValue];
	}
	
	nd = [el getChildByName:@"id"];
	if(nd){
		self.tid = [[nd getChildByName:@"#TEXT"] getValue];
	}
	
	nd = [el getChildByName:@"text"];
	if(nd){
		// self.text = [[[nd getChildByName:@"#TEXT"] getValue] retain];
		Element *exx = (Element *)nd;
		NSArray *es = [exx getElementsByName:@"#TEXT"];
		if(es){
			NSMutableString *str = [[NSMutableString alloc] init];
			for(Node *dn in es){
				[str appendString:[dn getValue]];
			}
			self.text = str;
			[str release];
		}		
	}
	
	nd = [el getChildByName:@"source"];
	if(nd){
		self.source = [[nd getChildByName:@"#TEXT"] getValue];
	}
	
	nd = [el getChildByName:@"truncated"];
	if(nd){
		NSString *ns = [[nd getChildByName:@"#TEXT"] getValue];
		NSNumber *n1 = [[NSNumber alloc] initWithBool:[ns isEqualToString:@"true"]];
		self.truncated = n1;
		[n1 release];
	}
	
	nd = [el getChildByName:@"favorited"];
	if(nd){
		NSString *ns = [[nd getChildByName:@"#TEXT"] getValue];
		NSNumber *n1 = [[NSNumber alloc] initWithBool:[ns isEqualToString:@"true"]];
		self.favorited = n1;
		[n1 release];
	}
	
	nd = [el getChildByName:@"user"];
	if(nd){
		DataManager *dm = [DataManager sharedManager];
		NSManagedObjectContext *context = dm.managedObjectContext;
		// User *u = [[[User alloc] init] autorelease];
		User *u = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:context];
		[u parse:(Element *)nd];
		self.usr = u;
	}
}

- (NSString *)created_at 
{
    NSString * tmpValue;
    
    [self willAccessValueForKey:@"created_at"];
    tmpValue = [self primitiveValueForKey:@"created_at"];
    [self didAccessValueForKey:@"created_at"];
    
    return tmpValue;
}

- (void)setCreated_at:(NSString *)value 
{
    [self willChangeValueForKey:@"created_at"];
    [self setPrimitiveValue:value forKey:@"created_at"];
    [self didChangeValueForKey:@"created_at"];
}

- (BOOL)validateCreated_at:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSNumber *)favorited 
{
    NSNumber * tmpValue;
    
    [self willAccessValueForKey:@"favorited"];
    tmpValue = [self primitiveValueForKey:@"favorited"];
    [self didAccessValueForKey:@"favorited"];
    
    return tmpValue;
}

- (void)setFavorited:(NSNumber *)value 
{
    [self willChangeValueForKey:@"favorited"];
    [self setPrimitiveValue:value forKey:@"favorited"];
    [self didChangeValueForKey:@"favorited"];
}

- (BOOL)validateFavorited:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSString *)source 
{
    NSString * tmpValue;
    
    [self willAccessValueForKey:@"source"];
    tmpValue = [self primitiveValueForKey:@"source"];
    [self didAccessValueForKey:@"source"];
    
    return tmpValue;
}

- (void)setSource:(NSString *)value 
{
    [self willChangeValueForKey:@"source"];
    [self setPrimitiveValue:value forKey:@"source"];
    [self didChangeValueForKey:@"source"];
}

- (BOOL)validateSource:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSString *)text 
{
    NSString * tmpValue;
    
    [self willAccessValueForKey:@"text"];
    tmpValue = [self primitiveValueForKey:@"text"];
    [self didAccessValueForKey:@"text"];
    
    return tmpValue;
}

- (void)setText:(NSString *)value 
{
    [self willChangeValueForKey:@"text"];
    [self setPrimitiveValue:value forKey:@"text"];
    [self didChangeValueForKey:@"text"];
}

- (BOOL)validateText:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSString *)tid 
{
    NSString * tmpValue;
    
    [self willAccessValueForKey:@"tid"];
    tmpValue = [self primitiveValueForKey:@"tid"];
    [self didAccessValueForKey:@"tid"];
    
    return tmpValue;
}

- (void)setTid:(NSString *)value 
{
    [self willChangeValueForKey:@"tid"];
    [self setPrimitiveValue:value forKey:@"tid"];
    [self didChangeValueForKey:@"tid"];
}

- (BOOL)validateTid:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSNumber *)truncated 
{
    NSNumber * tmpValue;
    
    [self willAccessValueForKey:@"truncated"];
    tmpValue = [self primitiveValueForKey:@"truncated"];
    [self didAccessValueForKey:@"truncated"];
    
    return tmpValue;
}

- (void)setTruncated:(NSNumber *)value 
{
    [self willChangeValueForKey:@"truncated"];
    [self setPrimitiveValue:value forKey:@"truncated"];
    [self didChangeValueForKey:@"truncated"];
}

- (BOOL)validateTruncated:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}


- (User *)usr 
{
    id tmpObject;
    
    [self willAccessValueForKey:@"usr"];
    tmpObject = [self primitiveValueForKey:@"usr"];
    [self didAccessValueForKey:@"usr"];
    
    return tmpObject;
}

- (void)setUsr:(User *)value 
{
    [self willChangeValueForKey:@"usr"];
    [self setPrimitiveValue:value forKey:@"usr"];
    [self didChangeValueForKey:@"usr"];
}


- (BOOL)validateUsr:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (NSNumber *)unreaded 
{
    NSNumber * tmpValue;
    
    [self willAccessValueForKey:@"unreaded"];
    tmpValue = [self primitiveValueForKey:@"unreaded"];
    [self didAccessValueForKey:@"unreaded"];
    
    return tmpValue;
}

- (void)setUnreaded:(NSNumber *)value 
{
    [self willChangeValueForKey:@"unreaded"];
    [self setPrimitiveValue:value forKey:@"unreaded"];
    [self didChangeValueForKey:@"unreaded"];
}

- (BOOL)validateUnreaded:(id *)valueRef error:(NSError **)outError 
{
    // Insert custom validation logic here.
    return YES;
}

- (void)dealloc{
	[created_at release];
	[tid release];
	[text release];
	[source release];
	[usr release];
	[truncated release];
	[unreaded release];
	[favorited release];
	[super dealloc];
}

@end
